﻿// Copyright (c)  YISH. All rights reserved.
// Licensed under the MIT License, See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Linq;
using NSQL.Sharpen;

namespace NSQL.Expressions.Functions
{
    public class ConcatFunction : BaseExpression, IConcatFunction
    {
        public IList<IStringExpression> Items { get; set; }

        public ConcatFunction(IList<IStringExpression> items)
        {
            if (items == null)
            {
                throw new ArgumentNullException(nameof(items));
            }
            
            if (items.Count <= 0)
            {
                throw new Exception("Concat must have at least one item");
            }

            Items = items;
        }
        
        public override T Accept<T>(IExpressionVisitor<T> visitor) => visitor.Visit(this);
        public override void Accept(IExpressionVisitor visitor) => visitor.Visit(this);
        public override bool IsValid => Items.All(o => o.IsValid);
        protected override IExpression DeepCopy()
        {
            return new ConcatFunction(Items.Select(o=>o.DeepClone()).ToList())
            {
                Tag = Tag
            };
        }
    }
}